package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.CheckgroupDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.CheckgroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 *
 * @author zhangmeng
 * @description 检查组service实现类
 * @date 2019/9/16
 **/
@Slf4j
@Service
public class CheckgroupServiceImpl implements CheckgroupService {
    @Resource
    private CheckgroupDao checkgroupDao;

    @Transactional
    @Override
    public void add(CheckGroup checkGroup) {
        log.info("[检查组-添加]data：{}",checkGroup);
        checkgroupDao.insert(checkGroup);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[检查组-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<CheckGroup> page = checkgroupDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Transactional
    @Override
    public void delete(int id) {
        log.info("[检查组-根据id删除]id：{}",id);
        //校验是否可以删除
        Long count = checkgroupDao.countCheckGroupByCheckGroupId(id);
        if(count >0){
            throw new BusinessRuntimeException("当前检查组有数据，不能删除");
        }
        //实际删除操作
        checkgroupDao.deleteById(id);
    }

    @Transactional
    @Override
    public void edit(CheckGroup checkGroup) {
        log.info("[检查组-编辑]data:{}",checkGroup);
        checkgroupDao.update(checkGroup);
    }

    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据ID查询]id:{}",id);
        return checkgroupDao.selectById(id);
    }
}
